package com.hanthink.quartz.util;

import mob.push.api.builder.PushWorkBuilder;
import mob.push.api.client.push.PushV3Client;
import mob.push.api.config.MobPushConfig;
import mob.push.api.exception.ApiException;
import mob.push.api.http.Result;
import mob.push.api.model.Push;
import mob.push.api.res.PushV3Res;
import mob.push.api.utils.SetUtil;

import java.util.UUID;

public class MobPushUtils {
    /**
     * 发送消息
     *
     * @param workNo 随便一个UUID
     * @param title 标题
     * @param userID 用户ID（支持多人）
     * @param content 内容
     * @return
     */
    private static Result<PushV3Res> pushAllAndroid(String workNo, String[] userID, String title, String content) {
        Push push = (new PushWorkBuilder()).setTargetAll(workNo, title, content).build();
        push.getPushNotify().setPlats(SetUtil.newSet(new Integer[]{1}));
        if (userID.length > 0) {
            //push.getSource().
            push.getPushTarget().setTarget(2);
            push.getPushTarget().setAlias(SetUtil.newSet(userID));
        }
        Result<PushV3Res> pushV3ResResult = PushV3Client.pushTaskV3(push);
        if (pushV3ResResult != null) {
            //log.info(DataUtils.toJson(pushV3ResResult));
        }
        return pushV3ResResult;
    }

    //发送正式服
    private static Result<PushV3Res> pushAllIOSDis(String workNo, String[] userID, String title, String content) {
        Push push = (new PushWorkBuilder()).setTargetAll(workNo, title, content).build();
        push.getPushNotify().setIosProduction(Push.IOS_PROD);
        push.getPushNotify().setPlats(SetUtil.newSet(new Integer[]{2}));
        if (userID.length > 0) {
            //push.getSource().
            push.getPushTarget().setTarget(2);
            push.getPushTarget().setAlias(SetUtil.newSet(userID));
        }
        Result<PushV3Res> pushV3ResResult = PushV3Client.pushTaskV3(push);
        if (pushV3ResResult != null) {
            //log.info(DataUtils.toJson(pushV3ResResult));
        }
        return pushV3ResResult;
    }

    //发送测试服
    private static Result<PushV3Res> pushAllIOSDev(String workNo, Long[] userID, String title, String content) {
        Push push = (new PushWorkBuilder()).setTargetAll(workNo, title, content).build();
        push.getPushNotify().setIosProduction(Push.IOS_DEV);
        push.getPushNotify().setPlats(SetUtil.newSet(new Integer[]{2}));
        if (userID.length>0) {
            //push.getSource().
            push.getPushTarget().setTarget(2);
            push.getPushTarget().setAlias(SetUtil.newSet(String.valueOf(userID)));
        }
        Result<PushV3Res> pushV3ResResult = PushV3Client.pushTaskV3(push);
        if (pushV3ResResult != null) {
            //log.info(DataUtils.toJson(pushV3ResResult));
        }
        return pushV3ResResult;
    }

        //发送全部

    /**
     *
     * @param title 标题
     * @param message 内容
     * @param AppKey AppKey
     * @param AppSecret AppSecret
     */
    public static void pushAll(String title, String message,String AppKey,String AppSecret) {

        MobPushConfig.appkey = AppKey;
        MobPushConfig.appSecret = AppSecret;

        try {
            //Registration ID推送
            //Result<PushV3Res> resResult = PushV3Client.pushByRids("workNo", "title", "content", "rid");
            String uuid = UUID.randomUUID().toString().replaceAll("-", "");
            System.out.println(uuid);
//            //发送测试
//            pushAllIOSDev(uuid,null,title,message);
//            uuid = UUID.randomUUID().toString().replaceAll("-","");
//            System.out.println(uuid);
            //发送正式
            pushAllIOSDis(uuid,null,title,message);

            uuid = UUID.randomUUID().toString().replaceAll("-", "");
            System.out.println(uuid);
            //发送Android
            pushAllAndroid(uuid, null, title, message);

        } catch (ApiException e) {
            e.getStatus();           //错误请求状态码
            e.getErrorCode();       //错误状态码
            e.getErrorMessage();   //错误信息
        }

    }

    public static void pushUser(String[] userId, String title, String message,String AppKey,String AppSecret) {
        MobPushConfig.appkey = AppKey;
        MobPushConfig.appSecret = AppSecret;

        try {
            String uuid = UUID.randomUUID().toString().replaceAll("-", "");
            System.out.println(uuid);
            //发送测试
//            pushAllIOSDev(uuid,userId,title,message);
//            uuid = UUID.randomUUID().toString().replaceAll("-","");
//            System.out.println(uuid);
            //发送正式
            pushAllIOSDis(uuid,userId,title,message);

//            uuid = UUID.randomUUID().toString().replaceAll("-","");
//            System.out.println(uuid);
            //发送Android
            pushAllAndroid(uuid, userId, title, message);

        } catch (ApiException e) {
            e.getStatus();           //错误请求状态码
            e.getErrorCode();       //错误状态码
            e.getErrorMessage();   //错误信息
        }
    }

    public static void main(String[] args) {
//        pushUser(Long.parseLong("3144"),"java 测试消息", "5月20苹果中","AppKey","AppSecret");
    }
}
